package com.da.javatest.thread.juc.work;

import java.util.UUID;

public class TraceUtil {

    public static final String REQUEST_HEADER_TRACE_ID = "com.ms.header.trace.id";
    public static final String MDC_TRACE_ID = "trace_id";

    private static InheritableThreadLocal<String> inheritableThreadLocal = new InheritableThreadLocal<>();

    /**
     * 获取traceid
     *
     * @return
     */
    public static String get() {
        String traceId = inheritableThreadLocal.get();
        if (traceId == null) {
            traceId = UUID.randomUUID().toString().replace("-", "");
            inheritableThreadLocal.set(traceId);
        }
        return traceId;
    }

    public static void set(String traceId) {
        inheritableThreadLocal.set(traceId);
    }

    public static void remove() {
        inheritableThreadLocal.remove();
    }

}